home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / dpmigcc5.zip / RSX / SOURCE / LIBC.C < prev    next >
C/C++ Source or Header  |  1994-12-12  |  11KB  |  474 lines

  1. /*****************************************************************************
  2.  * FILE: libc.c                                  *
  3.  *                                         *
  4.  * DESC:                                     *
  5.  *    - kernel libc functions                          *
  6.  *                                         *
  7.  * Copyright (C) 1993,1994                             *
  8.  *    Rainer Schnitker, Heeper Str. 283, 33607 Bielefeld             *
  9.  *    email: rainer@mathematik.uni-bielefeld.de                 *
  10.  *                                         *
  11.  *****************************************************************************/
  12.  
  13. #include <string.h>
  14. #include <malloc.h>
  15. #include <sys\types.h>
  16. #include <ctype.h>
  17.  
  18. #if !defined (NULL)
  19. #define NULL ((void *)0)
  20. #endif
  21.  
  22. #if !defined (_SIZE_T)
  23. #define _SIZE_T
  24. typedef unsigned long size_t;
  25. #endif
  26.  
  27. unsigned errno;
  28. unsigned _doserrno;
  29. unsigned _psp;
  30.  
  31. size_t strlen(__const__ char *s)
  32. {
  33.     register int __res __asm__("cx");
  34.     __asm__("cld\n\t"
  35.     "repne\n\t"
  36.     "scasb\n\t"
  37.     "notl %0\n\t"
  38.     "decl %0"
  39.     :"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff):"di");
  40.     return __res;
  41. }
  42.  
  43. char * strcat(char *dest, __const__ char *src)
  44. {
  45.     __asm__("cld\n\t"
  46.     "repne\n\t"
  47.     "scasb\n\t"
  48.     "decl %1\n"
  49.     "1:\tlodsb\n\t"
  50.     "stosb\n\t"
  51.     "testb %%al,%%al\n\t"
  52.     "jne 1b"
  53.     : /* no output */
  54.     :"S" (src),"D" (dest),"a" (0),"c" (0xffffffff):"si","di","ax","cx");
  55.     return dest;
  56. }
  57.  
  58. int strcmp(__const__ char *cs, __const__ char *ct)
  59. {
  60.     register int __res __asm__("ax");
  61.     __asm__("cld\n"
  62.     "1:\tlodsb\n\t"
  63.     "scasb\n\t"
  64.     "jne 2f\n\t"
  65.     "testb %%al,%%al\n\t"
  66.     "jne 1b\n\t"
  67.     "xorl %%eax,%%eax\n\t"
  68.     "jmp 3f\n"
  69.     "2:\tmovl $1,%%eax\n\t"
  70.     "jb 3f\n\t"
  71.     "negl %%eax\n"
  72.     "3:"
  73.     :"=a" (__res):"D" (cs),"S" (ct):"si","di");
  74.     return __res;
  75. }
  76.  
  77. char *strcpy(char *dest, __const__ char *src)
  78. {
  79.     __asm__("cld\n"
  80.     "1:\tlodsb\n\t"
  81.     "stosb\n\t"
  82.     "testb %%al,%%al\n\t"
  83.     "jne 1b"
  84.     : /* no output */
  85.     :"S" (src),"D" (dest):"si","di","ax","memory");
  86.     return dest;
  87. }
  88.  
  89. char *strstr (const char *string1, const char *string2)
  90. {
  91.   int len1, len2, i;
  92.   char first;
  93.  
  94.   if (*string2 == 0)
  95.     return ((char *)string1);
  96.   len1 = 0;
  97.   while (string1[len1] != 0) ++len1;
  98.   len2 = 0;
  99.   while (string2[len2] != 0) ++len2;
  100.   if (len2 == 0)
  101.     return ((char *)(string1+len1));
  102.   first = *string2;
  103.   while (len1 >= len2)
  104.     {
  105.       if (*string1 == first)
  106.     {
  107.       for (i = 1; i < len2; ++i)
  108.         if (string1[i] != string2[i])
  109.           break;
  110.       if (i >= len2)
  111.         return ((char *)string1);
  112.     }
  113.       ++string1; --len1;
  114.     }
  115.   return (NULL);
  116. }
  117.  
  118. int memcmp(__const__ void *cs, __const__ void *ct, size_t count)
  119. {
  120.     register int __res __asm__("ax");
  121.     __asm__("cld\n\t"
  122.     "repe\n\t"
  123.     "cmpsb\n\t"
  124.     "je 1f\n\t"
  125.     "movl $1,%%eax\n\t"
  126.     "jb 1f\n\t"
  127.     "negl %%eax\n"
  128.     "1:"
  129.     :"=a" (__res):"0" (0),"D" (cs),"S" (ct),"c" (count)
  130.     :"si","di","cx");
  131.     return __res;
  132. }
  133.  
  134. extern char **environ;
  135. char *getenv(char *name)
  136. {
  137.     int len;
  138.     char **p, *s;
  139.  
  140.     if (name == NULL || environ == NULL)
  141.     return (NULL);
  142.     len = strlen(name);
  143.     for (p = environ; *p != NULL; ++p) {
  144.     s = *p;
  145.     if (strlen(s) > len && s[len] == '=' && (memcmp(name, s, len) == 0))
  146.         return (s + len + 1);
  147.     }
  148.     return (NULL);
  149. }
  150.  
  151. void *memset(void *s, int c, size_t count)
  152. {
  153.     __asm__(
  154.     "cld\n\t"
  155.     "rep\n\t"
  156.     "stosb"
  157.     : /* no output */
  158.     :"a" (c),"D" (s),"c" (count)
  159.     :"cx","di","memory");
  160.     return s;
  161. }
  162.  
  163. void *memcpy(void *to, __const__ void *from, size_t n)
  164. {
  165.     __asm__(
  166.     "cld\n\t"
  167.     "movl %%edx, %%ecx\n\t"
  168.     "shrl $2,%%ecx\n\t"
  169.     "rep ; movsl\n\t"
  170.     "testb $1,%%dl\n\t"
  171.     "je 1f\n\t"
  172.     "movsb\n"
  173.     "1:\ttestb $2,%%dl\n\t"
  174.     "je 2f\n\t"
  175.     "movsw\n"
  176.     "2:\n"
  177.     : /* no output */
  178.     :"d" (n),"D" ((long) to),"S" ((long) from)
  179.     : "cx","di","si","memory");
  180.     return (to);
  181. }
  182.  
  183. static inline void bzero(void *p, unsigned size)
  184. {
  185.     __asm__ __volatile__(
  186.     "cld ; rep ; stosl"
  187.     :
  188.     :"a"(0), "D"((long) p), "c"(size >> 2)
  189.     :"di", "cx");
  190. }
  191.  
  192. void *sbrk(int bytes)
  193. {
  194.     static brk_value = 0x1000;
  195.     static brk_max = 0xA000;
  196.     int retv;
  197.  
  198.     if (bytes > 0xFFFF)
  199.     return (void *) -1;
  200.     if (brk_value + bytes > brk_max)
  201.     return (void *) -1;
  202.  
  203.     retv = brk_value;
  204.     brk_value += bytes;
  205.     return (void *) retv;
  206. }
  207.  
  208. #define MAGIC (Header *)1111
  209.  
  210. typedef long Align;
  211. union header {
  212.     struct {
  213.     union header *ptr;
  214.     unsigned size;
  215.     } s;
  216.     Align x;
  217. };
  218. typedef union header Header;
  219.  
  220. static Header base;
  221. static Header *freep = NULL;
  222.  
  223. void *malloc(size_t nbytes)
  224. {
  225.     Header *p, *prevp;
  226.     static Header *morecore(unsigned);
  227.     unsigned nunits;
  228.  
  229.     nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;
  230.  
  231.     if ((prevp = freep) == NULL) {
  232.     base.s.ptr = freep = prevp = &base;
  233.     base.s.size = 0;
  234.     }
  235.     for (p = prevp->s.ptr;; prevp = p, p = p->s.ptr) {
  236.     if (p->s.size >= nunits) {
  237.         if (p->s.size == nunits)
  238.         prevp->s.ptr = p->s.ptr;
  239.         else {
  240.         p->s.size -= nunits;
  241.         p += p->s.size;
  242.         p->s.size = nunits;
  243.         }
  244.         freep = prevp;
  245.  
  246.         p->s.ptr = MAGIC;
  247.  
  248.  
  249.         return (void *) (p + 1);
  250.     }
  251.     if (p == freep)
  252.         if ((p = morecore(nunits)) == NULL)
  253.         return NULL;
  254.     }
  255. }
  256.  
  257. #define NALLOC 128        /* 128 * Header = 1 KB */
  258.  
  259. static Header *morecore(unsigned nu)
  260. {
  261.     void *cp, *sbrk(int);
  262.     Header *up;
  263.  
  264.     if (nu < NALLOC)
  265.     nu = NALLOC;
  266.     cp = sbrk(nu * sizeof(Header));
  267.     if (cp == (void *) -1)
  268.     return NULL;
  269.     up = (Header *) cp;
  270.     up->s.size = nu;
  271.     up->s.ptr = MAGIC;
  272.     free((void *) (up + 1));
  273.     return freep;
  274. }
  275.  
  276. void free(void *ap)
  277. {
  278.     Header *bp, *p;
  279.  
  280.     bp = (Header *) ap - 1;
  281.  
  282.     if (bp->s.ptr != MAGIC)
  283.     return;
  284.     for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
  285.     if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
  286.         break;
  287.  
  288.     if (bp + bp->s.size == p->s.ptr) {
  289.     bp->s.size += p->s.ptr->s.size;
  290.     bp->s.ptr = p->s.ptr->s.ptr;
  291.     } else
  292.     bp->s.ptr = p->s.ptr;
  293.     if (p + p->s.size == bp) {
  294.     p->s.size += bp->s.size;
  295.     p->s.ptr = bp->s.ptr;
  296.     } else
  297.     p->s.ptr = bp;
  298.     freep = p;
  299. }
  300.  
  301. void *realloc(void *oldp, size_t newsize)
  302. {
  303.     void *newp;
  304.     Header *h;
  305.     unsigned oldsize;
  306.  
  307.     if (oldp == NULL)
  308.     return (malloc(newsize));
  309.  
  310.     h = (Header *) oldp - 1;
  311.     if (h->s.ptr != MAGIC)
  312.     return (NULL);
  313.  
  314.     oldsize = h->s.size * sizeof(Header) - sizeof(Header);
  315.  
  316.     free(oldp);
  317.     newp = malloc(newsize);
  318.     if (newp == NULL)
  319.     return NULL;
  320.     if (newp != oldp)
  321.     memcpy(newp, oldp, (newsize < oldsize) ? newsize : oldsize);
  322.     return (newp);
  323. }
  324.  
  325. void *calloc(size_t nelem, size_t size)
  326. {
  327.     void *p = malloc(nelem * size);
  328.  
  329.     if (p)
  330.     bzero(p, nelem * size);
  331.     return p;
  332. }
  333.  
  334. unsigned char _ctype[257] =
  335.       {
  336. /* -1 */    0,
  337. /* 00 */    _CNTRL,
  338. /* 01 */    _CNTRL,
  339. /* 02 */    _CNTRL,
  340. /* 03 */    _CNTRL,
  341. /* 04 */    _CNTRL,
  342. /* 05 */    _CNTRL,
  343. /* 06 */    _CNTRL,
  344. /* 07 */    _CNTRL,
  345. /* 08 */    _CNTRL,
  346. /* 09 */    _CNTRL|_SPACE,
  347. /* 0A */    _CNTRL|_SPACE,
  348. /* 0B */    _CNTRL|_SPACE,
  349. /* 0C */    _CNTRL|_SPACE,
  350. /* 0D */    _CNTRL|_SPACE,
  351. /* 0E */    _CNTRL,
  352. /* 0F */    _CNTRL,
  353. /* 10 */    _CNTRL,
  354. /* 11 */    _CNTRL,
  355. /* 12 */    _CNTRL,
  356. /* 13 */    _CNTRL,
  357. /* 14 */    _CNTRL,
  358. /* 15 */    _CNTRL,
  359. /* 16 */    _CNTRL,
  360. /* 17 */    _CNTRL,
  361. /* 18 */    _CNTRL,
  362. /* 19 */    _CNTRL,
  363. /* 1A */    _CNTRL,
  364. /* 1B */    _CNTRL,
  365. /* 1C */    _CNTRL,
  366. /* 1D */    _CNTRL,
  367. /* 1E */    _CNTRL,
  368. /* 1F */    _CNTRL,
  369. /* 20 */    _PRINT|_SPACE,
  370. /* 21 */    _PRINT|_PUNCT,
  371. /* 22 */    _PRINT|_PUNCT,
  372. /* 23 */    _PRINT|_PUNCT,
  373. /* 24 */    _PRINT|_PUNCT,
  374. /* 25 */    _PRINT|_PUNCT,
  375. /* 26 */    _PRINT|_PUNCT,
  376. /* 27 */    _PRINT|_PUNCT,
  377. /* 28 */    _PRINT|_PUNCT,
  378. /* 29 */    _PRINT|_PUNCT,
  379. /* 2A */    _PRINT|_PUNCT,
  380. /* 2B */    _PRINT|_PUNCT,
  381. /* 2C */    _PRINT|_PUNCT,
  382. /* 2D */    _PRINT|_PUNCT,
  383. /* 2E */    _PRINT|_PUNCT,
  384. /* 2F */    _PRINT|_PUNCT,
  385. /* 30 */    _PRINT|_DIGIT|_XDIGIT,
  386. /* 31 */    _PRINT|_DIGIT|_XDIGIT,
  387. /* 32 */    _PRINT|_DIGIT|_XDIGIT,
  388. /* 33 */    _PRINT|_DIGIT|_XDIGIT,
  389. /* 34 */    _PRINT|_DIGIT|_XDIGIT,
  390. /* 35 */    _PRINT|_DIGIT|_XDIGIT,
  391. /* 36 */    _PRINT|_DIGIT|_XDIGIT,
  392. /* 37 */    _PRINT|_DIGIT|_XDIGIT,
  393. /* 38 */    _PRINT|_DIGIT|_XDIGIT,
  394. /* 39 */    _PRINT|_DIGIT|_XDIGIT,
  395. /* 3A */    _PRINT|_PUNCT,
  396. /* 3B */    _PRINT|_PUNCT,
  397. /* 3C */    _PRINT|_PUNCT,
  398. /* 3D */    _PRINT|_PUNCT,
  399. /* 3E */    _PRINT|_PUNCT,
  400. /* 3F */    _PRINT|_PUNCT,
  401. /* 40 */    _PRINT|_PUNCT,
  402. /* 41 */    _PRINT|_UPPER|_XDIGIT,
  403. /* 42 */    _PRINT|_UPPER|_XDIGIT,
  404. /* 43 */    _PRINT|_UPPER|_XDIGIT,
  405. /* 44 */    _PRINT|_UPPER|_XDIGIT,
  406. /* 45 */    _PRINT|_UPPER|_XDIGIT,
  407. /* 46 */    _PRINT|_UPPER|_XDIGIT,
  408. /* 47 */    _PRINT|_UPPER,
  409. /* 48 */    _PRINT|_UPPER,
  410. /* 49 */    _PRINT|_UPPER,
  411. /* 4A */    _PRINT|_UPPER,
  412. /* 4B */    _PRINT|_UPPER,
  413. /* 4C */    _PRINT|_UPPER,
  414. /* 4D */    _PRINT|_UPPER,
  415. /* 4E */    _PRINT|_UPPER,
  416. /* 4F */    _PRINT|_UPPER,
  417. /* 50 */    _PRINT|_UPPER,
  418. /* 51 */    _PRINT|_UPPER,
  419. /* 52 */    _PRINT|_UPPER,
  420. /* 53 */    _PRINT|_UPPER,
  421. /* 54 */    _PRINT|_UPPER,
  422. /* 55 */    _PRINT|_UPPER,
  423. /* 56 */    _PRINT|_UPPER,
  424. /* 57 */    _PRINT|_UPPER,
  425. /* 58 */    _PRINT|_UPPER,
  426. /* 59 */    _PRINT|_UPPER,
  427. /* 5A */    _PRINT|_UPPER,
  428. /* 5B */    _PRINT|_PUNCT,
  429. /* 5C */    _PRINT|_PUNCT,
  430. /* 5D */    _PRINT|_PUNCT,
  431. /* 5E */    _PRINT|_PUNCT,
  432. /* 5F */    _PRINT|_PUNCT,
  433. /* 60 */    _PRINT|_PUNCT,
  434. /* 61 */    _PRINT|_LOWER|_XDIGIT,
  435. /* 62 */    _PRINT|_LOWER|_XDIGIT,
  436. /* 63 */    _PRINT|_LOWER|_XDIGIT,
  437. /* 64 */    _PRINT|_LOWER|_XDIGIT,
  438. /* 65 */    _PRINT|_LOWER|_XDIGIT,
  439. /* 66 */    _PRINT|_LOWER|_XDIGIT,
  440. /* 67 */    _PRINT|_LOWER,
  441. /* 68 */    _PRINT|_LOWER,
  442. /* 69 */    _PRINT|_LOWER,
  443. /* 6A */    _PRINT|_LOWER,
  444. /* 6B */    _PRINT|_LOWER,
  445. /* 6C */    _PRINT|_LOWER,
  446. /* 6D */    _PRINT|_LOWER,
  447. /* 6E */    _PRINT|_LOWER,
  448. /* 6F */    _PRINT|_LOWER,
  449. /* 70 */    _PRINT|_LOWER,
  450. /* 71 */    _PRINT|_LOWER,
  451. /* 72 */    _PRINT|_LOWER,
  452. /* 73 */    _PRINT|_LOWER,
  453. /* 74 */    _PRINT|_LOWER,
  454. /* 75 */    _PRINT|_LOWER,
  455. /* 76 */    _PRINT|_LOWER,
  456. /* 77 */    _PRINT|_LOWER,
  457. /* 78 */    _PRINT|_LOWER,
  458. /* 79 */    _PRINT|_LOWER,
  459. /* 7A */    _PRINT|_LOWER,
  460. /* 7B */    _PRINT|_PUNCT,
  461. /* 7C */    _PRINT|_PUNCT,
  462. /* 7D */    _PRINT|_PUNCT,
  463. /* 7E */    _PRINT|_PUNCT,
  464. /* 7F */    _CNTRL,
  465. /* 80 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  466. /* 90 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  467. /* A0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  468. /* B0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  469. /* C0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  470. /* D0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  471. /* E0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  472. /* F0 */    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  473.       };
  474.